<?php
/** 初始化项目 */
require '../../app/app.php';
/** 初始化数据库 */
use app\util\Mysql;
$database = Mysql::init();

$username = $_SESSION['username'];
if (isset($_POST["tid"]) && isset($_POST["textname"]) && isset($_POST["main"]) && isset($_POST["status"]) && isset($_POST["gread"]) && isset($_POST["otherread"]) && $_POST["moreid"] != "") {
    $tid = $_POST["tid"];
    $textname = $_POST["textname"];
    $main = $_POST["main"];
    $status = $_POST["status"];
    $gread = $_POST["gread"];
    $otherread = $_POST["otherread"];
    $moreid = $_POST["moreid"];
    if ($status == "on") {
        $status = "是";
    } else {
        $status = "否";
    }
    if ($gread == 2) {
        $gread = "可读写";
    } else if ($gread == 1) {
        $gread = "可读";
    } else {
        $gread = "不可读";
    }
    if ($otherread == 2) {
        $otherread = "可读写";
    } else if ($otherread == 1) {
        $otherread = "可读";
    } else {
        $otherread = "不可读";
    }
    $database->update("text_main", ["textname" => $textname, "main" => $main, "created" => time(), "status" => $status], ["tid" => $tid]);
    $database->update("text_auth", ["gread" => $gread, "otherread" => $otherread], ["tid" => $tid]);
    $database->update("more_file", ["text_id" => NULL, "file_tmp" => "是"], ["text_id" => $tid]);
    $database->update("more_file", ["text_id" => $tid, "file_tmp" => "否"], ["id" => $moreid]);
    echo "添加成功";
} else if (isset($_POST["tid"]) && isset($_POST["textname"]) && isset($_POST["main"]) && isset($_POST["status"]) && isset($_POST["gread"]) && isset($_POST["otherread"])) {
    $tid = $_POST["tid"];
    $textname = $_POST["textname"];
    $main = $_POST["main"];
    $status = $_POST["status"];
    $gread = $_POST["gread"];
    $otherread = $_POST["otherread"];
    if ($status == "on") {
        $status = "是";
    } else {
        $status = "否";
    }
    if ($gread == 2) {
        $gread = "可读写";
    } else if ($gread == 1) {
        $gread = "可读";
    } else {
        $gread = "不可读";
    }
    if ($otherread == 2) {
        $otherread = "可读写";
    } else if ($otherread == 1) {
        $otherread = "可读";
    } else {
        $otherread = "不可读";
    }
    $database->update("text_main", ["textname" => $textname, "main" => $main, "created" => time(), "status" => $status], ["tid" => $tid]);
    $database->update("text_auth", ["gread" => $gread, "otherread" => $otherread], ["tid" => $tid]);
    echo "添加成功";
} else if (isset($_POST["tid"]) && isset($_POST["textname"]) && isset($_POST["main"]) && isset($_POST["gread"]) && isset($_POST["otherread"]) && $_POST["moreid"] != "") {
    $tid = $_POST["tid"];
    $textname = $_POST["textname"];
    $main = $_POST["main"];
    $gread = $_POST["gread"];
    $otherread = $_POST["otherread"];
    $moreid = $_POST["moreid"];
    if ($status == "on") {
        $status = "是";
    } else {
        $status = "否";
    }
    if ($gread == 2) {
        $gread = "可读写";
    } else if ($gread == 1) {
        $gread = "可读";
    } else {
        $gread = "不可读";
    }
    if ($otherread == 2) {
        $otherread = "可读写";
    } else if ($otherread == 1) {
        $otherread = "可读";
    } else {
        $otherread = "不可读";
    }
    $database->update("text_main", ["textname" => $textname, "main" => $main, "created" => time(), "status" => $status], ["tid" => $tid]);
    $database->update("text_auth", ["gread" => $gread, "otherread" => $otherread], ["tid" => $tid]);
    $database->update("more_file", ["text_id" => NULL, "file_tmp" => "是"], ["text_id" => $tid]);
    $database->update("more_file", ["text_id" => $tid, "file_tmp" => "否"], ["id" => $moreid]);
    echo "添加成功";
} else if (isset($_POST["tid"]) && isset($_POST["textname"]) && isset($_POST["main"]) && isset($_POST["gread"]) && isset($_POST["otherread"])) {
    $tid = $_POST["tid"];
    $textname = $_POST["textname"];
    $main = $_POST["main"];
    $gread = $_POST["gread"];
    $otherread = $_POST["otherread"];
    if ($status == "on") {
        $status = "是";
    } else {
        $status = "否";
    }
    if ($gread == 2) {
        $gread = "可读写";
    } else if ($gread == 1) {
        $gread = "可读";
    } else {
        $gread = "不可读";
    }
    if ($otherread == 2) {
        $otherread = "可读写";
    } else if ($otherread == 1) {
        $otherread = "可读";
    } else {
        $otherread = "不可读";
    }
    $database->update("text_main", ["textname" => $textname, "main" => $main, "created" => time(), "status" => $status], ["tid" => $tid]);
    $database->update("text_auth", ["gread" => $gread, "otherread" => $otherread], ["tid" => $tid]);
    echo "添加成功";
} else {
    echo "参数错误";
}
